<?php

class PartyMasterController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
		$party=Party::get();

		//return Response::json(Party::get());
        if($party){
            $message = 'Party Successfully retrieved.';
            return Response::json(ResponseManager::getResult($party->toArray(), $message));
        } 
        else {
            $message = 'Ooopss...! Party can\'t get.';
            return Response::json(ResponseManager::getError(array(), 01, $message));
        }
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//

	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
		$input=Input::all();

		$rules = array(
			'firmname'=>'required',
			'email'=>'unique:users',
			'partyname'=>'required',
			'phone'=>'required',
			'landline'=>'required',
			'fax'=>'required',
			'email'=>'required',
			'website'=>'required',
			'tin'=>'required',
			'cst'=>'required',
			'gst'=>'required',
			'address'=>'required',
			'city'=>'required',
			'state'=>'required',
			'country'=>'required',
			'creditterm'=>'required',
			'creditlimit'=>'required',
			'blacklist'=>'required',
			'note'=>'required'
			);

    	$validator = Validator::make($input, $rules);

    		if ($validator->fails())
    		{
    			$message = $validator->messages()->first();
                return Response::json(ResponseManager::getError(array(), '01', $message));
        		//return Redirect::to('hello')->withErrors($validator);
    		}else{
    		 	$party=Party::create($input);
   						if($party){
        					    $message = 'Party Successfully retrieved.';
            					return Response::json(ResponseManager::getResult($party->toArray(), $message));
        		   				  } 
        				else {
        		    			$message = 'Ooopss...! Party can\'t get.';
            					return Response::json(ResponseManager::getError(array(), 01, $message));
        					}		

	}
}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//

		$party=Party::find($id);
	 		if($party){
        			$message = 'Party Successfully retrieved.';
            			return Response::json(ResponseManager::getResult($party->toArray(), $message));
        		   		  } 
        		else {
        		    	$message = 'Ooopss...! Party can\'t get.';
            			return Response::json(ResponseManager::getError(array(), 01, $message));
        			 }        
	
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
				$party=Party::find($id);
		 		if($party){
        			$message = 'Party Successfully retrieved.';
            			return Response::json(ResponseManager::getResult($party->toArray(), $message));
        		   		  } 
        		else {
        		    	$message = 'Ooopss...! Party can\'t get.';
            			return Response::json(ResponseManager::getError(array(), 01, $message));
        			 }        

	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
		$input=Input::all();
		$party=Party::find($id);
		//$abc=Input::all();
	foreach ($input as $key => $value) {

            $party->value = $value;
            $party->save();
        }
	if($party){
      	$message = 'Party Successfully retrieved.';
            return Response::json(ResponseManager::getResult($party->toArray(), $message));
         	  } 
        	else {
        		$message = 'Ooopss...! Party can\'t get.';
            		return Response::json(ResponseManager::getError(array(), 01, $message));
        		}
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
		  // $party = DB::table('partymaster')    //'students'
    //             ->where('partyid', '=', $id)
    //              ->get();
		 //$party = Party::where('partyid', '=', $id)->SoftDeletes();
		$party=Party::find($id);

		if($party){
			$party->delete();	
		}
         // $party = DB::table('partymaster')
         //            ->where('partyid',$id)
         //            ->SoftDeletes();

         // $party = $table::where('partyid',$id)
         //                ->get();
       // $party = Party::all();

        if ($party) {
            $message = 'Deleted Successfully.';
            return Response::json(ResponseManager::getResult($party, $message));
        } else {
            $message = 'Ooopss...! Not deleted.';
            return Response::json(ResponseManager::getError(array(), 01, $message));
        }
	}


}
